<template>
  <div class="container">
    <div class="headerimg">
      <router-link to="/myblog" class="text1">personal blog</router-link>
      <div class="text2">路漫漫其修远兮,吾将上下而求索</div>
      <div class="text3">
        <router-link to="/myblog">首页</router-link>
        <!-- <a @click.prevent="linkclick('新闻')">新闻</a>
        <a @click.prevent="linkclick('科技')">科技</a>
        <a @click.prevent="linkclick('旅行')">旅行</a> -->
        <router-link to="">留言</router-link>
        <a @click.prevent="layout">退出</a>
      </div>
    </div>
    <el-card class="card1">
      <a @click="getoneblog">推荐</a>
      <a :to="`/myblog`" v-for="item in categories" :key="item.id" @click.prevent="getonecategory(item.alias)">{{ item.alias }}</a>
    </el-card>
    <div class="main">
      <div class="rankblog">
        <el-card class="card2">
          <h3 style="font-weight: bold">博客阅读量排行榜</h3>
          <div v-for="(item, index) in rankblog" :key="item.id" style="width: 100%; margin-bottom: 7px; font-weight: bold">
            <a @click.prevent="rankview(item.id)">
              <span>{{ index + 1 }}.</span>{{ item.tittle }}</a
            >
          </div>
        </el-card>
      </div>
      <div class="news">
        <el-card>
          <h1 style="font-size: 24px; color: #333; text-align: center; padding-bottom: 5px">{{ oneblog.tittle }}</h1>
          <div class="newcontent" v-html="oneblog.content"></div>
          <footer>
            <div>
              <span
                >作者: <strong>{{ oneblog.author }}</strong></span
              >
              <span
                >创建时间: <em>{{ oneblog.create_time }}</em></span
              >
              <span
                >浏览次数: <em>{{ oneblog.views }}</em></span
              >
            </div>
          </footer>
        </el-card>
      </div>

      <div class="blogaside">
        <el-card>
          <p class="searchtext">搜索文章</p>
          <div class="search-container">
            <input type="text" class="search-input" placeholder="请输入关键字搜素" v-model="searchmsg" />
            <button class="search-button" @click="searchcontent">搜索</button>

            <p class="search-hint">搜素文章内容关键字</p>
          </div>
          <div class="newblog">
            <h3 style="border-bottom: 1px solid black; width: 112px; margin-bottom: 5px; font-weight: bold; font-size: 16px;margin-top: 20px;">最新发布的博客</h3>
            <div class="newtittle" v-for="item in newblog" :key="item.id">
              <a @click.prevent="latestblog(item.id)"> {{ item.tittle }}</a>
            </div>
          </div>
          <div class="newblog" style="margin-top: 20px">
            <h3 style="border-bottom: 1px solid black; width: 179px; font-weight: bold; margin-bottom: 5px; font-size: 16px">为您推荐相关分类的博客</h3>
            <div class="newtittle" v-for="item in correlateblog" :key="item.id">
              <a @click.prevent="latestblog(item.id)"> {{ item.tittle }}</a>
            </div>
          </div>
        </el-card>
      </div>
    </div>
    <!-- <router-view></router-view> -->
  </div>
</template>

<script>
export default {
  name: 'BackEndMyBlog',
  data() {
    return {
      categories: [],
      rankblog: [],
      oneblog: [],
      newblog: [],
      tagcategories: [],
      page: 1,
      size: 5,
      total: 0,
      searchmsg: '',
      correlateblog: []
    }
  },

  mounted() {},
  async created() {
    const id = this.$route.params.id
    const blog = await this.$http.get(`/article/${id}`)
    this.oneblog = blog.result[0]
    const views = blog.result[0].views
    const categoryid = blog.result[0].category_id
    const correlate = await this.$http.get(`/article/correlate/${categoryid}`)
    this.correlateblog = correlate.result.data
    this.$http.put(`/article/view/${id}`, { views: views })
    const {
      result: { data }
    } = await this.$http.get('/categories/all')

    this.categories = data
    this.categories.forEach(item => {
      this.tagcategories.push(item)
    })

    const { result } = await this.$http.get('/article/all')
    this.rankblog = result.data.sort(function (a, b) {
      b.views - a.views
    })
    const res = await this.$http.get('/article/newblog')
    this.newblog = res.result.data
  },
  methods: {
    removeTag(tag) {
      const index = this.tagcategories.indexOf(tag)
      if (index !== -1) {
        this.tagcategories.splice(index, 1)
      }
    },
    tagclick(tag) {
      console.log(tag)
    },
    async rankview(val) {
      const id = val
      const { result } = await this.$http.get(`/article/${id}`)
      this.oneblog = result[0]
    },
    async latestblog(val) {
      const id = val
      const { result } = await this.$http.get(`/article/${id}`)
      this.oneblog = result[0]
    },
    searchcontent() {
      if (this.searchmsg == '') {
        this.oneblog.content = this.oneblog.content
        return
      }
      const regex = new RegExp(this.searchmsg, 'ig')
      this.oneblog.content = this.oneblog.content.replace(regex, `<span style="color: red;font-weight: bold;">$&</span>`)
      this.searchmsg = ''
    },
    layout() {
      this.$message({
        type: 'success',
        message: '退出成功',
        duration: 1000
      })
      localStorage.removeItem('token')
      localStorage.removeItem('ismanager')
      this.$router.push('/login')
    },
    async getoneblog() {
      const { result } = await this.$http.get('/article/oneblog')
      this.oneblog=result.data[0]
    },
    async getonecategory(val){
      const { result } = await this.$http.get('/article/oneblog',{ params: {category:val }})
      this.oneblog=result.data[0]
    }
  }
}
</script>

<style scoped>
.headerimg {
  position: relative;
  width: 100%;
  /* background-color: #ddf; */
  height: 250px;
  background-image: url('../../assets/images/preview.jpg');
  background-repeat: no-repeat;
  background-size: cover;
  background-attachment: fixed;
  background-position: center center;
}
.text1 {
  text-decoration: none;
  font-size: 20px;
  color: #fff;
  position: relative;
  left: 20px;
  top: 20px;
  font-weight: 900;
  font-family: 'WenQuanYi Micro Hei';
}
.text1:hover {
  color: rgb(230, 217, 217);
}
.text2 {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  color: rgb(163, 153, 153);
  font-size: 30px;
}
.text3 {
  float: right;
  position: relative;
  left: -20px;
  top: 20px;
}
.text3 a {
  text-decoration: none;
  font-size: 20px;
  color: #fff;
  font-weight: 700;
  font-family: 'WenQuanYi Micro Hei';
  padding: 0 10px;
}
.text3 a:hover {
  color: rgb(230, 217, 217);
}
.card1 a {
  text-decoration: none;
  font-size: 16px;
  font-family: 'WenQuanYi Micro Hei';
  padding: 0 10px;
  font-weight: 500;
  color: rgb(66, 64, 64);
  cursor: pointer;
}
.card2 {
  display: flex;
  /* width: 15%; */
  height: 410px;
  background-color: #f9f9f9;
  margin-top: 10px;
  margin-left: 20px;
}
.card2 a {
  text-decoration: none;
  font-size: 13px;
  color: black;
}
.card2 a:hover {
  color: #1ea3f6;
}
.card2 a span {
  font-size: 15px;
}
.card2 h3 {
  margin-bottom: 10px;
}
.main {
  display: flex;
  justify-content: space-between;
}
.news {
  width: 60%;
  margin-left: 20px;
  margin-top: 10px;
}
.blogaside {
  /* flex: 1; */
  width: 25%;
  margin-top: 10px;
  margin-left: 20px;
  min-width: 300px;
}
.texts {
  display: flex;
}
.blogtexts {
  margin-left: 5px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.search-form {
  display: flex;
  align-items: center;
}

.search-input {
  flex: 1;
  height: 32px;
  padding: 0 10px;
  border: 1px solid #ccc;
  border-radius: 16px;
  font-size: 14px;
}

.search-button {
  height: 32px;
  padding: 0 16px;
  border: none;
  background: #00bcd4;
  color: #fff;
  font-size: 14px;
  cursor: pointer;
}

.search-hint {
  margin-top: 10px;
  font-size: 12px;
  color: #999;
}
.searchtext {
  width: 65px;
  border-bottom: 1px solid #000;
  margin-bottom: 5px;
  font-weight: bold;
}
.hot-tags-container {
  margin-left: -20px;
  padding: 20px;
  background: #fff;
}
.hot-tags-container .el-tag {
  margin-right: 10px;
  margin-bottom: 10px;
  cursor: pointer;
}
.hot-tags-container .el-tag:hover {
  border-color: #00bcd4;
  color: #00bcd4;
}
.newtittle {
  padding-bottom: 8px;
  font-size: 13px;
  font-weight: bold;
}
.newblog a {
  text-decoration: none;
  color: black;
}
.newblog a:hover {
  color: #1ea3f6;
}
.rankblog {
  width: 400px;
}
.newcontent {
  font-size: 16px;
  color: #666;
  line-height: 1.5;
  text-indent: 2em;
  border-top: 1px solid #ccc;
  padding-top: 1em;
}
footer {
  margin-top: 2em;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

footer > div > span {
  font-size: 14px;
  padding-left: 10px;
  color: #888;
}

footer > div > span > strong {
  color: #333;
}

footer > div > span > em {
  font-style: italic;
}
</style>
